package cn.edu.ecut.controller;

import cn.edu.ecut.dao.StudentDao;
import cn.edu.ecut.entity.Student;
import com.mozicoding.foundation.support.GraphicHelper;
import com.mozicoding.web.context.ParameterHolder;
import com.mozicoding.web.context.RequestHolder;
import com.mozicoding.web.context.ResponseHolder;
import com.mozicoding.web.support.Controller;
import com.mozicoding.web.support.RedirectAttributes;
import com.mozicoding.web.support.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;

@Controller(path = "/student")

public class StudentController {
    private static final String ERROR_NAME="errorMassage";
    private static final String CAPTCHA_NAME="captcha";
    private StudentDao studentDao=new StudentDao();
    @RequestMapping(path = "/page/sign/up")
    public String studentSignUpPage(){

      return "/WEB-INF/assets/sign-up.html";
    }
    @RequestMapping(path = "/page/sign/in")
    public String studentSignInPage(){

        return "/WEB-INF/assets/sign-in.html";
    }
    private boolean validate(ParameterHolder holder,RedirectAttributes attrs){
        String identifier=holder.getString("loginName");
        if (identifier==null||identifier.trim().isEmpty()){
            attrs.addFlashAttribute(ERROR_NAME,"名称不能为空");
            return false;
        }
        String passwd1=holder.getString("password");
        if (passwd1==null||passwd1.trim().isEmpty()){
            attrs.addFlashAttribute(ERROR_NAME,"密码不能为空");
            return false;
        }
        String passwd2=holder.getString("comfirm");
        if (passwd2==null||passwd2.trim().isEmpty()){
            attrs.addFlashAttribute(ERROR_NAME,"你没有再次确认密码");
            return false;
        }
        String captchaInput=holder.getString("captcha");
        if (captchaInput==null||captchaInput.trim().isEmpty()){
            attrs.addFlashAttribute(ERROR_NAME,"验证码不能为空");
            return false;
        }

        if (!passwd1.trim().equals(passwd2.trim())){
            attrs.addFlashAttribute(ERROR_NAME,"两次密码不一致!");
            return false;
        }
        HttpServletRequest request =RequestHolder.getCurrentRequest();
        HttpSession session=request.getSession();
        String code= (String) session.getAttribute(CAPTCHA_NAME);
        if (!code.trim().equalsIgnoreCase(captchaInput.trim())){
            attrs.addFlashAttribute(ERROR_NAME,"验证码错误!");
            return false;
        }
        return true;
    }
    @RequestMapping(path="/action/sign/up")
    public String signUpAction(ParameterHolder holder, RedirectAttributes attrs){
        final String fail="redirect:/student/page/sign/up";
        final String success="redirect:/student/page/sign/in";
        if (!this.validate(holder, attrs)){
            return fail;
        }
        String logName=holder.getString("loginName");
        String password=holder.getString("password");
        Student s=new Student();
        Boolean x=studentDao.save(s);
        s.setLoginName(logName);
        s.setPassword(password);
        if (x){
            return success;
        }
        else  {
            attrs.addFlashAttribute(ERROR_NAME,"注册失败！");
            return fail;
        }

    }
    @RequestMapping(path = "/captcha")
    public void Captcha(HttpSession session){
        HttpServletResponse response= ResponseHolder.getCurrentResponse();
        try {
            OutputStream output=response.getOutputStream();
            String code= GraphicHelper.create(6,false,180,40,output,50);
            System.out.println("生成的验证码为："+code);
            session.setAttribute(CAPTCHA_NAME,code);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
